/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Yahtzee;

/**
 *
 * @author agreseth fo real
 */
import java.net.*;
import java.io.*;
import javax.swing.*;
public class MultiServer {

    ServerSocket serverSocket = null;
    boolean listening = true;
    int port;
    String receieved;
    ServerGUI gui;
    String[] ipAddress;
    int count = 0;
    String[] gamePlayer1;
    String[] gamePlayer2;
    int gameCount = 0;
    boolean[] waiting;
    int[] test;
    private HighScores highScore;
    private File stats;
    private int scores[];
    private String namesArray[];

    
    MultiServerThread[] t;

    public MultiServer(int funport, ServerGUI gu)
    {
        port = funport;
        this.gui = gu;
        gamePlayer1 = new String[200];
        gamePlayer2 = new String[200];
        waiting = new boolean[200];
        test = new int[200];
        t = new MultiServerThread[200];
        highScore = new HighScores();
    }
    public void start()
    {
        try
        {

            serverSocket = new ServerSocket(port);
        }
        catch (IOException e)
        {
            System.err.println("Could not listen on port: " + port);
            System.exit(-1);
        }

        ipAddress = new String[200];
        count = 0;
        while (listening)
        {
            try
            {
                //test
                
                JOptionPane.showMessageDialog(null, "Yo");
//                JFrame f = new JFrame();
//
//                JDialog dialog = new JDialog(f, serverSocket.getLocalSocketAddress().toString(), true);
//                dialog.dispose();
                //gui.startServerButton.setEnabled(true);
                
                
                
                count++;
                // sets up the user, giving it its own thread stored in a thread array
                t[count-1] = new MultiServerThread(serverSocket.accept(),this.gui,this);
                t[count-1].start();
                System.out.println("I made it homeie");
            }
            catch (IOException e)
            {
                System.err.println("Problem with the thread...");
            }
        }


    }

        
    public String[] getList()
    {
        //ipAddress[0] = "/123.456.789.000";
        return ipAddress;
    }

    // set up the game, associating ips with one another 
    public int setUpGame(String ip1, String ip2)
    {
        System.out.println("Player1 " + ip1);
        System.out.println("Player2 " + ip2);
        gameCount++;
        gamePlayer1[gameCount] = ip1;
        gamePlayer2[gameCount] = ip2;

        int player = findIp("/"+ip2);
        System.out.println("This "+player);
        t[player].startGame(ip1);
        waiting[player] = false;
        test[player] = 1;
        return 1;
    }

    // finds the specified ip in the list
    private int findIp(String ip)
    {
        for (int i = 0; i < count; i++)
        {
            System.out.println(i+" "+ipAddress[i]);
        }
        int i = 0;
        while(i < count)
        {
            if (ipAddress[i].equals(ip))
                return i;
            else
                i++;
        }
        return 0;
    }
    public String receive()
    {
        return receieved;
    }
    public void addIP(String test)
    {
        
        ipAddress[count - 1] = test;
    }
    public void close()
    {
        try
        {
            serverSocket.close();
        }
        catch (IOException e)
        {
            System.err.println("Problem closing the socket...");
        }
    }

    // depreciated
    public int waiting(String ip)
    {
        int c = findIp(ip);
        System.out.println(c);
        System.out.println(test[c]);
        //setting it to 0 to bug test
        if (test[c-1] == 1)
            return 1;
        else
            return 0;
    }

    // finds the thread associated with the game, and sends the other user the score
    // based on category
    public void setScores(String input, int score, String category)
    {

        // gets the ip of the person involved
        String ip = input.substring(1);
        int game = 0;
        int player = 0;
        // loops through determining who this person is, who they are playing, and
        // what game they are in
        for(int i = 1; i < gameCount+1;i++)
        {
            System.out.println(i + " "+gamePlayer1[i]);
            System.out.println(i+" "+gamePlayer2[i]);
            if(gamePlayer1[i].equalsIgnoreCase(ip))
            {
                game = i;
                player = 2;
            }
            else if(gamePlayer2[i].equalsIgnoreCase(ip))
            {
                game = i;
                player = 1;
            }
        }
        // set up the person to contact
        String person = "";
        if (player == 1)
        {
            person = gamePlayer1[game];
        }
        else
        {
            person = gamePlayer2[game];
        }
        System.out.println(person);
        int c = findIp("/"+person);
        System.out.println(c);
        // send the category and score off to that thread, which will send to its client
        t[c].send(category, score);
        
    }
    
    
        
}